package com.pinyoumei.app.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.pinyoumei.app.R;
import com.pinyoumei.app.bean.GoodDetailsBean;
import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;
import com.zzhoujay.richtext.RichText;
import com.zzhoujay.richtext.ig.DefaultImageDownloader;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2021-08-13.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2021-08-13        Administrator             新增Person类
 * 2021-08-13        Administrator             增加sex属性
 * 开团商品详情
 */
public class ProductDetailPtActivity extends BaseActivity implements View.OnClickListener {

    private Banner banner;
    private List<String> image_list = new ArrayList<>();
    private GoodDetailsBean goodDetailsBean;
    private int num = 1;
    private TextView tvMarketPrice;
    private TextView tvBeginGroupBuyTime;
    private TextView tvPtPrice;
    private TextView tvCountDownHour;
    private TextView tvGoodsName;
    private CircleImageView ivNumBg1;
    private CircleImageView ivNumBg2;
    private CircleImageView ivNumBg3;
    private CircleImageView ivNumBg4;
    private TextView tvSaleNum;
    private TextView tvStockNum;
    private TextView tvEquipmentDesc;
    private String goods_id = "";
    private TextView tv_group_buying_num;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail_pt);
        goodDetailsBean = (GoodDetailsBean) getIntent().getSerializableExtra("goodDetailsBean");
        goods_id = getIntent().getStringExtra("goods_id");


        InitWidget();

        InitData();
    }

    private void InitWidget() {
        RichText.initCacheDir(this);
        RichText.debugMode = true;

        banner = findViewById(R.id.banner);

        ImageView iv_close = findViewById(R.id.iv_close);
        iv_close.setOnClickListener(this);

        Button bt_buy = findViewById(R.id.bt_buy);
        bt_buy.setOnClickListener(this);

        tvMarketPrice = findViewById(R.id.tv_market_price);
        tvBeginGroupBuyTime = findViewById(R.id.tv_begin_group_buy_time);
        tvPtPrice = findViewById(R.id.tv_pt_price);
        tvCountDownHour = findViewById(R.id.tv_count_down_hour);
        tvGoodsName = findViewById(R.id.tv_goods_name);
        ivNumBg1 = findViewById(R.id.iv_num_bg_1);
        ivNumBg2 = findViewById(R.id.iv_num_bg_2);
        ivNumBg3 = findViewById(R.id.iv_num_bg_3);
        ivNumBg4 = findViewById(R.id.iv_num_bg_4);
        tvSaleNum = findViewById(R.id.tv_sale_num);
        tvStockNum = findViewById(R.id.tv_stock_num);
        tvEquipmentDesc = findViewById(R.id.tv_equipment_desc);
        tv_group_buying_num = findViewById(R.id.tv_group_buying_num);

    }

    private void InitData() {
        if (goodDetailsBean != null) {

            if (goodDetailsBean.getResult().getData_info().getBanner_images() != null) {
                String[] tags = goodDetailsBean.getResult().getData_info().getBanner_images().split(",");

                if (tags != null && tags.length > 0) {
                    for (int i = 0; i < tags.length; i++) {
                        image_list.add(tags[i]);
                    }
                    initBanner();
                }
            }

            tvGoodsName.setText(goodDetailsBean.getResult().getData_info().getGoods_name() + "");
            tvMarketPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG); //中划线
            tvMarketPrice.setText("市场价：¥" + goodDetailsBean.getResult().getData_info().getMarket_price() + "");

            tvPtPrice.setText("开心团劵：" + goodDetailsBean.getResult().getData_info().getPt_price() + "");
            tvBeginGroupBuyTime.setText("开团时间：" + goodDetailsBean.getResult().getData_info().getBegin_group_buy_time() + "");

            tvCountDownHour.setText(goodDetailsBean.getResult().getData_info().getCount_down_hour() + "时" +
                    goodDetailsBean.getResult().getData_info().getCount_down_minute() + "分"+
                    goodDetailsBean.getResult().getData_info().getCount_down_second() + "秒");

            tvSaleNum.setText("销量：" + goodDetailsBean.getResult().getData_info().getSale_num() + "件");

            tvStockNum.setText("库存：" + goodDetailsBean.getResult().getData_info().getStock_num() + "件");
            tv_group_buying_num.setText("等" + goodDetailsBean.getResult().getData_info().getGroup_buying_num() + "人正在开团");


            List<String> user_head_imgs = goodDetailsBean.getResult().getData_info().getUser_head_imgs();
            if (user_head_imgs != null && user_head_imgs.size() > 0) {
                if (user_head_imgs.get(0) != null) {
                    Glide.with(this).load(user_head_imgs.get(0)).into(ivNumBg1);
                }

                if (user_head_imgs.get(1) != null) {
                    Glide.with(this).load(user_head_imgs.get(1)).into(ivNumBg2);
                }
                if (user_head_imgs.get(2) != null) {
                    Glide.with(this).load(user_head_imgs.get(2)).into(ivNumBg3);
                }
                if (user_head_imgs.get(3) != null) {
                    Glide.with(this).load(user_head_imgs.get(3)).into(ivNumBg4);
                }
            }

        }

        RichText.from(goodDetailsBean.getResult().getData_info().getGoods_detail())
                .autoFix(true)
                .showBorder(false)
                .autoPlay(true)
                .imageDownloader(new DefaultImageDownloader())
                .into(tvEquipmentDesc);
    }


    private void initBanner() {
        //第二个Banner

        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);//设置Banner的样式BannerConfig.CIRCLE_INDICATOR
        banner.setImages(image_list); //存放图片的list集合url
        banner.setBannerAnimation(Transformer.ZoomOutSlide);//设置Banner轮播的动画特效的样式
        //banner.setPageTransformer(true,)//自定义轮播动画
        banner.setImageLoader(new GlideImageLoader());//设置图片加载器
        banner.isAutoPlay(true);//设置默认为自动轮播
        banner.setDelayTime(3000);//设置轮播间隔时间
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);


        banner.start();

    }

    //使用Glide加载器
    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {

            //Glide 加载图片

            Glide.with(context).load(path).into(imageView);

//            //设置图片圆角角度
//            RoundedCorners roundedCorners = new RoundedCorners(DensityUtil.dp2px(5));
//            //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
//            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
//            Glide.with(context)
//                    .load(path)
//                    .apply(options)
//                    .into(imageView);

        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_close:
                finish();
                break;

            case R.id.bt_buy:

                //立即购买
                if (goodDetailsBean!=null){
                    Intent intent = new Intent(ProductDetailPtActivity.this,OrderPayActivity.class);
                    intent.putExtra("goods_id",goods_id);
                    intent.putExtra("goods_num",num);
                    intent.putExtra("order_type",1);
                    startActivity(intent);
                }/*{
                    num = 1;
                    BottomDialog bottomDialog = new BottomDialog(ProductDetailPtActivity.this,R.layout.dialog_buy);
                    bottomDialog.show();
                    ImageView iv_bg = bottomDialog.findViewById(R.id.iv_bg);
                    //Glide 加载图片
                    RequestOptions placeholder = new RequestOptions().placeholder(R.mipmap.ic_error_loading);
                    if (image_list!=null && image_list.size()>0){
                        Glide.with(ProductDetailPtActivity.this).load(image_list.get(0)).apply(placeholder).into(iv_bg);

                    }

                    EditText edittext_num = bottomDialog.findViewById(R.id.edittext_pos_num);
                    edittext_num.setEnabled(false);
                    edittext_num.setFocusableInTouchMode(false);
                    TextView tv_title = bottomDialog.findViewById(R.id.tv_title);
                    TextView tv_sale_amount = bottomDialog.findViewById(R.id.tv_sale_amount);
                    tv_title.setText(goodDetailsBean.getResult().getData_info().getGoods_name());

                    tv_sale_amount.setText(goodDetailsBean.getResult().getData_info().getSale_price()+"");


//                    RelativeLayout relative_remove = bottomDialog.findViewById(R.id.relative_remove);
//                    relative_remove.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            //点击减号
//                            if (Integer.valueOf(edittext_num.getText().toString().trim()) <= 1) {
//                                return;
//                            }
//                            int b = Integer.valueOf(edittext_num.getText().toString().trim()) - 1;
//                            edittext_num.setText(b + "");
//                            num = b;
//                        }
//                    });
//
//                    RelativeLayout relative_add = bottomDialog.findViewById(R.id.relative_add);
//                    relative_add.setOnClickListener(new View.OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            //点击加号
//                            int a = Integer.valueOf(edittext_num.getText().toString().trim()) + 1;
//                            edittext_num.setText(a + "");
//                            num = a;
//                        }
//                    });
                    Button bt_sure = bottomDialog.findViewById(R.id.bt_sure);
                    bt_sure.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            bottomDialog.dismiss();
                            Intent intent = new Intent(ProductDetailPtActivity.this,OrderPayActivity.class);
                            intent.putExtra("goods_id",goods_id);
                            intent.putExtra("goods_num",num);
                            intent.putExtra("order_type",1);
                            startActivity(intent);
                        }
                    });
                }*/


                break;


            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RichText.recycle();
    }
}
